package com.blog.cloud.service.rec.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.blog.cloud.comm.modal.pojo.User;
import com.blog.cloud.comm.webutils.DateUtils;
import com.blog.cloud.comm.webutils.GetWaterId;

public class UploadService {

	
	public static UploadService ser = new UploadService();
	
	
	
	public String uploadFile(File file , User user , String pathHeader){
		String currDate = DateUtils.getCurrDate("yyyy"+File.separator+"MM"+File.separator+"dd");
		String rootPath = pathHeader+ File.separator + currDate+File.separator+ user.getSerialId();
		
		File pathFile = new File(rootPath);
		if(!pathFile.exists()){
			pathFile.mkdirs();
		}
		
		
		String fileName = file.getName();
		String expandName = fileName.split("\\.")[1];
		String realFileName = GetWaterId.getWaterId()+"."+expandName;
		
		
		
		String realPath = rootPath+File.separator+ realFileName;
		
		File realFile = new File(realPath);
		
		int length = 0;
		try {
			FileOutputStream output = new FileOutputStream(realFile);
			FileInputStream input = new FileInputStream(file);
			
			length = input.available();

            // 创建缓存区域
            byte[] buffer = new byte[length];
            // 将文件中的数据写入缓存数组
            input.read(buffer);
            // 将缓存数组中的数据输出到文件
            output.write(buffer);
            output.close();
            input.close();
			file.delete();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return realPath;
	}
}
